/*
This softtware use BSD licence (http://opensource.org/licenses/BSD-3-Clause)

Copyright (c) 2013, Martin Lebeda
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the Martin Lebeda nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package cz.lebeda.bas;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;


/**
 * @author <a href="mailto:martin.lebeda@gmail.com">Martin Lebeda</a>
 *         Date: 11.11.13
 */
@Root
public class VOBackupFile {

    @Element
    private String path;
    @Element
    private long size;
    @Element
    private Date modify;
    @Element
    private boolean directory;

    @Element(required = false)
    private String fileHash;
    @Element(required = false)
    private String checkSumBck;

    public VOBackupFile() {
    }


    /**
     * Wrapper for metadata of backuped file.
     *
     * @param file backuped file
     */
    public VOBackupFile(File file) {
        this.path = file.getAbsolutePath();
        this.size = file.length();
        this.modify = new Date(file.lastModified());
        this.directory = file.isDirectory();
    }

    /**
     * Return if file is prepared for backup or not.
     *
     * @return true if file need prepare to backup
     */
    public boolean unPrepared() {
        return (size > 0) && (!isDirectory()) && (fileHash == null);
    }

    @Override
    public boolean equals(final Object other) {
        if (other != null && other instanceof VOBackupFile) {
            return new EqualsBuilder()
                    .append(this.getPath(), ((VOBackupFile) other).getPath())
                    .append(this.getModify(), ((VOBackupFile) other).getModify())
                    .append(this.getSize(), ((VOBackupFile) other).getSize())
                    .append(this.isDirectory(), ((VOBackupFile) other).isDirectory())
                    .isEquals();
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
               .append(path)
               .append(size)
               .append(modify)
               .append(directory)
               .toHashCode();
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
    }

    public void setFileHash(String fileHash) {
        this.fileHash = fileHash;
    }

    public void setDirectory(boolean directory) {
        this.directory = directory;
    }

    public void setModify(Date modify) {
        this.modify = modify;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPath() {
        return path;
    }

    public long getSize() {
        return size;
    }

    public Date getModify() {
        return modify;
    }

    public boolean isDirectory() {
        return directory;
    }

    public String getFileHash() {
        return fileHash;
    }

    public void setCheckSumBck(String checkSumBck) {
        this.checkSumBck = checkSumBck;
    }

    public String getCheckSumBck() {
        return checkSumBck;
    }
}
